package com.truongnha.clienttn.business.service;

import android.content.Context;
import com.truongnha.clienttn.business.model.UserModel;
import com.truongnha.clienttn.data.dao.AccountDAO;
import com.truongnha.clienttn.data.dao.CreateDatabase;
import com.truongnha.clienttn.data.entity.AccountEntity;
import com.truongnha.clienttn.data.sao.LoginSAO;

public class LoginService {
	private Context context;
	private String userName;
	private String passWord;
	private UserModel user;
	private boolean success = false;
	private LoginSAO loginSAO;

	public LoginService(Context context, String userName, String passWord) {
		super();
		this.context = context;
		this.userName = userName;
		this.passWord = passWord;
	}

	public void login() {
		loginSAO = new LoginSAO(context, userName, passWord);
		loginSAO.getUserFromServer();
		this.success = loginSAO.isSuccess();
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public UserModel getUser() {
		if (this.success) {
			CreateDatabase db = new CreateDatabase(context);
			AccountDAO accountDAO = new AccountDAO(db.open());
			AccountEntity accountEntity = accountDAO.getAccountByUser(userName);
			db.close();
			user = new UserModel(accountEntity);
		}
		return user;
	}
}
